//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.ObjectModel;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using OpenLS.Core.Serialization;

namespace OpenLS.Drawing
{
    ///<summary>
    /// Represents a picture in a document
    ///</summary>
    public class Picture : ShapeBase
    {
        private BlipFillProperties _blipFill;
        private PictureNonVisual _nvPicPr;
         

        private ShapeProperties _spPr = new ShapeProperties()
                                            {
                                                Geometry =
                                                    new PresetGeometry2D()
                                                        {
                                                            Type = ShapeType.Rectangle,
                                                            AdjustValueList = new ObservableCollection<GeometryGuide>()
                                                        }
                                            };
        private Stretch _stretch = Stretch.Fill;
        /// <summary>
        /// Initializes a new picture.
        /// </summary>
        public Picture()
        {
        }

        public  double DesiredHeight
        {
            get
            {
                var v = base.Height;
                if (v == null && BlipFillProperties != null && BlipFillProperties.Blip != null)
                    return BlipFillProperties.Blip.Source.Height;
                return base.Height.GetValueOrDefault(0);
            }
        }
        public  double DesiredWidth
        {
            get
            {
                var v = base.Width;
                if (v == null && BlipFillProperties != null && BlipFillProperties.Blip != null)
                    return BlipFillProperties.Blip.Source.Width;
                return v.GetValueOrDefault(0);
            }
        }
        
        public Picture(BitmapSource source): this()
        {
            SetImageSource(source);
        }


        public int NavigationId
        {
            get { return PictureNonVisual.NonVisualDrawingProps.Id; }
            set { PictureNonVisual.NonVisualDrawingProps.Id = value; }
        }

        public string Name
        {
            get { return PictureNonVisual.NonVisualDrawingProps.Name; }
            set { PictureNonVisual.NonVisualDrawingProps.Name = value; }
        }


        public ImageSource Source
        {
            get { return BlipFillProperties.Blip.Source; }
        }

        public System.Windows.Media.Stretch Stretch
        {
            get { return _stretch; }
            set { _stretch = value; NotifyPropertyChanged("Stretch");}
        }

        public PictureNonVisual PictureNonVisual
        {
            get
            {
                if (_nvPicPr == null)
                    _nvPicPr = new PictureNonVisual();
                return _nvPicPr;
            }
            set { _nvPicPr = value;
                NotifyPropertyChanged("PictureNonVisual"); }
        }

        public BlipFillProperties BlipFillProperties
        {
            get
            {
                if (_blipFill == null)
                    _blipFill = new BlipFillProperties();
                return _blipFill;
            }
            set { _blipFill = value; NotifyPropertyChanged("BlipFillProperties");}
        }

        public override ShapeProperties ShapeProperties
        {
            get
            {
                if (_spPr == null)
                    _spPr = new ShapeProperties();
                return _spPr;
            }
            set { _spPr = value; NotifyPropertyChanged("ShapeProperties");}
        }

        private void SetImageSource(BitmapSource src)
        {
            BlipFillProperties.Blip = new Blip {Source = src};
            ShapeProperties.SafeXfrm = new Transform2D { Size = new PositiveSize2D(src.Width, src.Height) };
            ShapeProperties.Geometry = new PresetGeometry2D();
        }

        protected override void ReadXmlCore(ReadContext context)
        {
            using (var c = context.Read("pic")) //\\, SpreadsheetConstants.SpreadsheetDrawingNamespaceUri))
            {
                PictureNonVisual = c.ReadElement<PictureNonVisual>();
                BlipFillProperties = c.ReadElement<BlipFillProperties>();
                _spPr = c.ReadElement<ShapeProperties>();
            }
        }

        protected override void WriteXmlCore(WriteContext context)
        {
            using (WriteContext c = context.Write("pic"))
            {
                c.WriteElement(PictureNonVisual);
                c.WriteElement(BlipFillProperties);
                c.WriteElement(ShapeProperties);
            }
        }
    }
}